/**
 * 
 */
package com.geekteam.manga.wiget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.geekteam.manga.R;

/**
 * @author MtRol
 * 
 */
public class ListviewLoadMore extends ListView implements OnScrollListener {

	protected RelativeLayout mFooterView;

	protected ProgressBar mProgressBarLoadMore;

	protected OnLoadMoreListener mOnLoadMoreListener;
	protected boolean mIsLoadingMore = true;
	protected int mCurrentScrollState;

	public ListviewLoadMore(Context context) {
		super(context);
		if(!isInEditMode()){
			initListView(context);
		}
	}

	public ListviewLoadMore(Context context, AttributeSet attrs) {
		super(context, attrs);
		if(!isInEditMode()){
			initListView(context);
		}
	}

	public ListviewLoadMore(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		if(!isInEditMode()){
		    initListView(context);
		}
	}

	public void initListView(Context context) {
		LayoutInflater mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mFooterView = (RelativeLayout) mInflater.inflate(
				R.layout.listview_load_more, this, false);
		mProgressBarLoadMore = (ProgressBar) mFooterView
				.findViewById(R.id.load_more_progressBar);

		addFooterView(mFooterView);

		super.setOnScrollListener(this);
	}

	@Override
	public void addHeaderView(View v) {
		super.addHeaderView(v);
	}

	@Override
	public void setAdapter(ListAdapter adapter) {
		super.setAdapter(adapter);
	}

	public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
		mOnLoadMoreListener = onLoadMoreListener;
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {

		if (mOnLoadMoreListener != null) {
			boolean loadMore = firstVisibleItem + visibleItemCount >= totalItemCount;

			if (!mIsLoadingMore && loadMore
					&& mCurrentScrollState != SCROLL_STATE_IDLE) {
				mProgressBarLoadMore.setVisibility(View.VISIBLE);
				mIsLoadingMore = true;
				onLoadMore();
			}

		}
	}

	public void showProgressBar(boolean show) {
		mProgressBarLoadMore.setVisibility(show?View.VISIBLE:View.GONE);
	}
	
	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		mCurrentScrollState = scrollState;
	}

	private void onLoadMore() {
		if (mOnLoadMoreListener != null) {
			mOnLoadMoreListener.onLoadMore();
		}
	}

	public void onLoadMoreComplete() {
		mIsLoadingMore = false;
		mProgressBarLoadMore.setVisibility(View.GONE);
	}

	public void onLoadTotalItem() {
		mIsLoadingMore = true;
		removeFooterView(mFooterView);
	}

	public interface OnLoadMoreListener {
		public void onLoadMore();
	}
	
	public void removeFooterView(){
		removeFooterView(mFooterView);
	}
}
